var prefix = ctx + "msg/messages";

$(function() {	
    var options = {
        url: prefix + "/page",
        updateUrl: prefix + "/goEditPage",
        removeUrl: prefix + "/{id}",
        modalName: "消息",
        columns: [
        {
            field: 'msgTitle', 
            title: '消息标题'
        },
        {
            field: 'msgType',
            title: '消息类型',
            formatter: function(value, row, index) {
                return $.table.selectDictLabel(msgTypeDatas, value);
            }
        },
        {
            field: 'receiveRange',
            title: '接收范围',
            formatter: function(value, row, index) {
                return value === '1'?"部分":"全部";
            }
        },
        {
            field: 'sendChannel',
            title: '发送方式',
            formatter: function(value, row, index) {
                var channelLabel = [];
                value.split(",").forEach(function (channel) {
                    var tempStr = $.table.selectDictLabel(sendTypeDatas, channel)
                    channelLabel.push(tempStr)
                })
                return channelLabel.join("|");
            }
        },
        {
            field: 'createTime',
            title: '创建时间'
        },
        {
            title: '操作',
            align: 'center',
            formatter: function(value, row, index) {
                var actions = [];
                if (row.msgStatus == '0') {
                    actions.push('<a class="btn btn-success btn-xs '+editFlag+'" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.msgId + '\')"><i class="fa fa-edit"></i>编辑</a>&nbsp;');
                    actions.push('<a class="btn btn-danger btn-xs '+deleteFlag+'"  href="javascript:void(0)" onclick="$.operate.remove(\'' + row.msgId + '\')"><i class="fa fa-remove"></i>删除</a>&nbsp;');
                    actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="sendMessage(\'' + row.msgId + '\')"><i class="glyphicon glyphicon-send"></i>发送</a>&nbsp;');
                }else if(row.msgStatus == '1'){
                    actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="$.operate.view(\'' + row.msgId + '\')"><i class="fa fa-search"></i>查看</a>&nbsp;');
                    actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="showResultById(\'' + row.msgId + '\')"><i class="glyphicon glyphicon-eye-open"></i>结果</a>&nbsp;');
                }
                return actions.join('');
            }
        }]
    };
    $.table.init(options);
});

function sendMessage(msgId) {
//    var option = {
//        url: prefix + "/send/"+ msgId,
//        method: "put",
//        dataType: 'json',
//        beforeSubmit: function () {
//            $.modal.loading("正在发送中，请稍后...");
//        },
//        success: function (response) {
//            if(response.code === 0) {
//                $.modal.alertSuccess("发送成功")
//                $.table.refresh()
//            }else {
//                $.modal.alertError(response.msg)
//            }
//        }
//    }
//    $.modal.confirm("确定发送该条" + table.options.modalName + "信息吗？", function() {
//        $.ajax(option)
//    });

    $.modal.confirm("确定发送该条" + table.options.modalName + "信息吗？", function() {
		$.operate.put(prefix + "/send/"+ msgId, '', function(){
			window.parent.loadNotReadMessage()
		});
    })
}

function showResultById(msgId) {
    localStorage.setItem("currentMsgId",msgId)
    $.modal.openTab("消息发送结果",prefix + "/goMessageResult/" + msgId)
}